package com.xhtt.modules.cfg.schedule;

import com.xhtt.common.utils.DateUtils;
import com.xhtt.common.utils.ListUtils;
import com.xhtt.common.utils.SpringContextUtils;
import com.xhtt.modules.cfg.entity.CfgHoseInfo;
import com.xhtt.modules.cfg.entity.CfgHoseLog;
import com.xhtt.modules.cfg.entity.CfgHoseProduct;
import com.xhtt.modules.cfg.service.CfgHoseInfoService;
import com.xhtt.modules.cfg.service.CfgHoseLogService;
import com.xhtt.modules.cfg.service.CfgHoseProductService;
import com.xhtt.modules.cfg.service.impl.CfgHoseInfoServiceImpl;
import com.xhtt.modules.cfg.service.impl.CfgHoseLogServiceImpl;
import com.xhtt.modules.cfg.service.impl.CfgHoseProductServiceImpl;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class HoseLog implements Runnable {
    private CfgHoseInfoService hoseInfoService = SpringContextUtils.getBean(CfgHoseInfoServiceImpl.class);
    private CfgHoseProductService hoseProductService = SpringContextUtils.getBean(CfgHoseProductServiceImpl.class);
    private CfgHoseLogService hoseLogService = SpringContextUtils.getBean(CfgHoseLogServiceImpl.class);

    private CfgHoseInfo current;
    private CfgHoseInfo old;
    private List<CfgHoseProduct> oldProductList;
    private String deptUser;

    private HoseLog() {
    }

    public HoseLog(CfgHoseInfo current, CfgHoseInfo old, List<CfgHoseProduct> oldProductList, String deptUser) {
        this.current = current;
        this.old = old;
        this.oldProductList = CollectionUtils.isEmpty(oldProductList) ? new ArrayList<>() : oldProductList;
        this.deptUser = deptUser;
    }

    @Override
    public void run() {
        String pressureLevelInfo = "";
        String rfidInfo = "";
        String dateScrapInfo = "";
        String dateSuppressInfo = "";
        String comProductInfo = "";
        String spareProductInfo = "";

        if (!ObjectUtils.nullSafeEquals(current.getPressureLevel(), old.getPressureLevel())) {
            pressureLevelInfo = (1 == old.getPressureLevel() ? "A类" : "B类") + " → " + (1 == current.getPressureLevel() ? "A类" : "B类");
        }
        if (!ObjectUtils.nullSafeEquals(current.getRfid(), old.getRfid())) {
            rfidInfo = old.getRfid() + " → " + current.getRfid();
        }
        if (!ObjectUtils.nullSafeEquals(current.getDateScrap(), old.getDateScrap())) {
            dateScrapInfo = DateUtils.df.format(old.getDateScrap()) + " → " + DateUtils.df.format(current.getDateScrap());
        }
        if (!ObjectUtils.nullSafeEquals(current.getDateSuppress(), old.getDateSuppress())) {
            dateSuppressInfo = DateUtils.df.format(old.getDateSuppress()) + " → " + DateUtils.df.format(current.getDateSuppress());
        }

        CfgHoseProduct curComProduct = null == current.getComProduct() ? new CfgHoseProduct() : current.getComProduct();
        CfgHoseProduct oldComProduct = oldProductList.stream().filter(p -> p.getType().intValue() == 1).findFirst().orElse(new CfgHoseProduct());
        if (!ObjectUtils.nullSafeEquals(curComProduct.getProductId(), oldComProduct.getProductId())) {
            comProductInfo = (null == oldComProduct.getName() ? "" : oldComProduct.getName()) + " → " + (null == curComProduct.getName() ? "" : curComProduct.getName());
        }

        List<CfgHoseProduct> curSpareProductList = null == current.getSpareProducts() ? new ArrayList<>() : current.getSpareProducts();
        List<String> curSpareProductNames = curSpareProductList.stream().map(p -> p.getName()).collect(Collectors.toList());
        oldProductList.removeIf(p -> p.getType().intValue() == 1);
        List<String> oldSpareProductNames = oldProductList.stream().map(p -> p.getName()).collect(Collectors.toList());
        if (!ListUtils.listEqualsCheck(curSpareProductNames, oldSpareProductNames)) {
            spareProductInfo = String.join(",", oldSpareProductNames) + " → " + String.join(",", curSpareProductNames);
        }

        if (StringUtils.isNotEmpty(pressureLevelInfo) || StringUtils.isNotEmpty(rfidInfo) || StringUtils.isNotEmpty(dateScrapInfo)
                || StringUtils.isNotEmpty(dateSuppressInfo) || StringUtils.isNotEmpty(comProductInfo) || StringUtils.isNotEmpty(spareProductInfo)) {
            CfgHoseLog log = new CfgHoseLog();
            log.setHoseId(current.getId());
            log.setDeptUser(deptUser);
            log.setPressureLevelInfo(pressureLevelInfo);
            log.setRfidInfo(rfidInfo);
            log.setDateScrapInfo(dateScrapInfo);
            log.setDateSuppressInfo(dateSuppressInfo);
            log.setComProductInfo(comProductInfo);
            log.setSpareProductInfo(spareProductInfo);
            hoseLogService.save(log);
        }

    }

}
